<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.2.4 - src/lstring.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - lstring.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.2.4</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">78</td>
            <td class="headerCovTableEntry">80</td>
            <td class="headerCovTableEntryHi">97.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:16</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: lstring.c,v 2.26.1.1 2013/04/12 18:48:47 roberto Exp $
<span class="lineNum">       3 </span>            : ** String table (keeps all strings handled by Lua)
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : #include &lt;string.h&gt;
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : #define lstring_c
<span class="lineNum">      11 </span>            : #define LUA_CORE
<span class="lineNum">      12 </span>            : 
<span class="lineNum">      13 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : #include &quot;lmem.h&quot;
<span class="lineNum">      16 </span>            : #include &quot;lobject.h&quot;
<span class="lineNum">      17 </span>            : #include &quot;lstate.h&quot;
<span class="lineNum">      18 </span>            : #include &quot;lstring.h&quot;
<span class="lineNum">      19 </span>            : 
<span class="lineNum">      20 </span>            : 
<span class="lineNum">      21 </span>            : /*
<span class="lineNum">      22 </span>            : ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
<span class="lineNum">      23 </span>            : ** compute its hash
<span class="lineNum">      24 </span>            : */
<span class="lineNum">      25 </span>            : #if !defined(LUAI_HASHLIMIT)
<span class="lineNum">      26 </span>            : #define LUAI_HASHLIMIT          5
<span class="lineNum">      27 </span>            : #endif
<span class="lineNum">      28 </span>            : 
<span class="lineNum">      29 </span>            : 
<span class="lineNum">      30 </span>            : /*
<span class="lineNum">      31 </span>            : ** equality for long strings
<span class="lineNum">      32 </span>            : */
<span class="lineNum">      33 </span><span class="lineCov">        573 : int luaS_eqlngstr (TString *a, TString *b) {</span>
<span class="lineNum">      34 </span><span class="lineCov">        573 :   size_t len = a-&gt;tsv.len;</span>
<span class="lineNum">      35 </span>            :   lua_assert(a-&gt;tsv.tt == LUA_TLNGSTR &amp;&amp; b-&gt;tsv.tt == LUA_TLNGSTR);
<span class="lineNum">      36 </span><span class="lineCov">        740 :   return (a == b) ||  /* same instance or... */</span>
<span class="lineNum">      37 </span><span class="lineCov">        167 :     ((len == b-&gt;tsv.len) &amp;&amp;  /* equal length and ... */</span>
<span class="lineNum">      38 </span><span class="lineCov">        135 :      (memcmp(getstr(a), getstr(b), len) == 0));  /* equal contents */</span>
<span class="lineNum">      39 </span>            : }
<span class="lineNum">      40 </span>            : 
<span class="lineNum">      41 </span>            : 
<span class="lineNum">      42 </span>            : /*
<span class="lineNum">      43 </span>            : ** equality for strings
<span class="lineNum">      44 </span>            : */
<span class="lineNum">      45 </span><span class="lineCov">     188447 : int luaS_eqstr (TString *a, TString *b) {</span>
<span class="lineNum">      46 </span><span class="lineCov">     376894 :   return (a-&gt;tsv.tt == b-&gt;tsv.tt) &amp;&amp;</span>
<span class="lineNum">      47 </span><span class="lineCov">     188447 :          (a-&gt;tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b));</span>
<span class="lineNum">      48 </span>            : }
<span class="lineNum">      49 </span>            : 
<span class="lineNum">      50 </span>            : 
<span class="lineNum">      51 </span><span class="lineCov">     119915 : unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {</span>
<span class="lineNum">      52 </span><span class="lineCov">     119915 :   unsigned int h = seed ^ cast(unsigned int, l);</span>
<span class="lineNum">      53 </span>            :   size_t l1;
<span class="lineNum">      54 </span><span class="lineCov">     119915 :   size_t step = (l &gt;&gt; LUAI_HASHLIMIT) + 1;</span>
<span class="lineNum">      55 </span><span class="lineCov">     915676 :   for (l1 = l; l1 &gt;= step; l1 -= step)</span>
<span class="lineNum">      56 </span><span class="lineCov">     795761 :     h = h ^ ((h&lt;&lt;5) + (h&gt;&gt;2) + cast_byte(str[l1 - 1]));</span>
<span class="lineNum">      57 </span><span class="lineCov">     119915 :   return h;</span>
<span class="lineNum">      58 </span>            : }
<span class="lineNum">      59 </span>            : 
<span class="lineNum">      60 </span>            : 
<span class="lineNum">      61 </span>            : /*
<span class="lineNum">      62 </span>            : ** resizes the string table
<span class="lineNum">      63 </span>            : */
<span class="lineNum">      64 </span><span class="lineCov">        492 : void luaS_resize (lua_State *L, int newsize) {</span>
<span class="lineNum">      65 </span>            :   int i;
<span class="lineNum">      66 </span><span class="lineCov">        492 :   stringtable *tb = &amp;G(L)-&gt;strt;</span>
<span class="lineNum">      67 </span>            :   /* cannot resize while GC is traversing strings */
<span class="lineNum">      68 </span><span class="lineCov">        492 :   luaC_runtilstate(L, ~bitmask(GCSsweepstring));</span>
<span class="lineNum">      69 </span><span class="lineCov">        492 :   if (newsize &gt; tb-&gt;size) {</span>
<span class="lineNum">      70 </span><span class="lineCov">        473 :     luaM_reallocvector(L, tb-&gt;hash, tb-&gt;size, newsize, GCObject *);</span>
<span class="lineNum">      71 </span><span class="lineCov">      54297 :     for (i = tb-&gt;size; i &lt; newsize; i++) tb-&gt;hash[i] = NULL;</span>
<span class="lineNum">      72 </span>            :   }
<span class="lineNum">      73 </span>            :   /* rehash */
<span class="lineNum">      74 </span><span class="lineCov">      69804 :   for (i=0; i&lt;tb-&gt;size; i++) {</span>
<span class="lineNum">      75 </span><span class="lineCov">      69312 :     GCObject *p = tb-&gt;hash[i];</span>
<span class="lineNum">      76 </span><span class="lineCov">      69312 :     tb-&gt;hash[i] = NULL;</span>
<span class="lineNum">      77 </span><span class="lineCov">     127700 :     while (p) {  /* for each node in the list */</span>
<span class="lineNum">      78 </span><span class="lineCov">      58388 :       GCObject *next = gch(p)-&gt;next;  /* save next */</span>
<span class="lineNum">      79 </span><span class="lineCov">      58388 :       unsigned int h = lmod(gco2ts(p)-&gt;hash, newsize);  /* new position */</span>
<span class="lineNum">      80 </span><span class="lineCov">      58388 :       gch(p)-&gt;next = tb-&gt;hash[h];  /* chain it */</span>
<span class="lineNum">      81 </span><span class="lineCov">      58388 :       tb-&gt;hash[h] = p;</span>
<span class="lineNum">      82 </span><span class="lineCov">      58388 :       resetoldbit(p);  /* see MOVE OLD rule */</span>
<span class="lineNum">      83 </span><span class="lineCov">      58388 :       p = next;</span>
<span class="lineNum">      84 </span>            :     }
<span class="lineNum">      85 </span>            :   }
<span class="lineNum">      86 </span><span class="lineCov">        492 :   if (newsize &lt; tb-&gt;size) {</span>
<span class="lineNum">      87 </span>            :     /* shrinking slice must be empty */
<span class="lineNum">      88 </span>            :     lua_assert(tb-&gt;hash[newsize] == NULL &amp;&amp; tb-&gt;hash[tb-&gt;size - 1] == NULL);
<span class="lineNum">      89 </span><span class="lineCov">         19 :     luaM_reallocvector(L, tb-&gt;hash, tb-&gt;size, newsize, GCObject *);</span>
<span class="lineNum">      90 </span>            :   }
<span class="lineNum">      91 </span><span class="lineCov">        492 :   tb-&gt;size = newsize;</span>
<span class="lineNum">      92 </span><span class="lineCov">        492 : }</span>
<span class="lineNum">      93 </span>            : 
<span class="lineNum">      94 </span>            : 
<span class="lineNum">      95 </span>            : /*
<span class="lineNum">      96 </span>            : ** creates a new string object
<span class="lineNum">      97 </span>            : */
<span class="lineNum">      98 </span><span class="lineCov">      44997 : static TString *createstrobj (lua_State *L, const char *str, size_t l,</span>
<span class="lineNum">      99 </span>            :                               int tag, unsigned int h, GCObject **list) {
<span class="lineNum">     100 </span>            :   TString *ts;
<span class="lineNum">     101 </span>            :   size_t totalsize;  /* total size of TString object */
<span class="lineNum">     102 </span><span class="lineCov">      44997 :   totalsize = sizeof(TString) + ((l + 1) * sizeof(char));</span>
<span class="lineNum">     103 </span><span class="lineCov">      44997 :   ts = &amp;luaC_newobj(L, tag, totalsize, list, 0)-&gt;ts;</span>
<span class="lineNum">     104 </span><span class="lineCov">      44997 :   ts-&gt;tsv.len = l;</span>
<span class="lineNum">     105 </span><span class="lineCov">      44997 :   ts-&gt;tsv.hash = h;</span>
<span class="lineNum">     106 </span><span class="lineCov">      44997 :   ts-&gt;tsv.extra = 0;</span>
<span class="lineNum">     107 </span><span class="lineCov">      44997 :   memcpy(ts+1, str, l*sizeof(char));</span>
<span class="lineNum">     108 </span><span class="lineCov">      44997 :   ((char *)(ts+1))[l] = '\0';  /* ending 0 */</span>
<span class="lineNum">     109 </span><span class="lineCov">      44997 :   return ts;</span>
<span class="lineNum">     110 </span>            : }
<span class="lineNum">     111 </span>            : 
<span class="lineNum">     112 </span>            : 
<span class="lineNum">     113 </span>            : /*
<span class="lineNum">     114 </span>            : ** creates a new short string, inserting it into string table
<span class="lineNum">     115 </span>            : */
<span class="lineNum">     116 </span><span class="lineCov">      41151 : static TString *newshrstr (lua_State *L, const char *str, size_t l,</span>
<span class="lineNum">     117 </span>            :                                        unsigned int h) {
<span class="lineNum">     118 </span>            :   GCObject **list;  /* (pointer to) list where it will be inserted */
<span class="lineNum">     119 </span><span class="lineCov">      41151 :   stringtable *tb = &amp;G(L)-&gt;strt;</span>
<span class="lineNum">     120 </span>            :   TString *s;
<span class="lineNum">     121 </span><span class="lineCov">      41151 :   if (tb-&gt;nuse &gt;= cast(lu_int32, tb-&gt;size) &amp;&amp; tb-&gt;size &lt;= MAX_INT/2)</span>
<span class="lineNum">     122 </span><span class="lineCov">        365 :     luaS_resize(L, tb-&gt;size*2);  /* too crowded */</span>
<span class="lineNum">     123 </span><span class="lineCov">      41151 :   list = &amp;tb-&gt;hash[lmod(h, tb-&gt;size)];</span>
<span class="lineNum">     124 </span><span class="lineCov">      41151 :   s = createstrobj(L, str, l, LUA_TSHRSTR, h, list);</span>
<span class="lineNum">     125 </span><span class="lineCov">      41151 :   tb-&gt;nuse++;</span>
<span class="lineNum">     126 </span><span class="lineCov">      41151 :   return s;</span>
<span class="lineNum">     127 </span>            : }
<span class="lineNum">     128 </span>            : 
<span class="lineNum">     129 </span>            : 
<span class="lineNum">     130 </span>            : /*
<span class="lineNum">     131 </span>            : ** checks whether short string exists and reuses it or creates a new one
<span class="lineNum">     132 </span>            : */
<span class="lineNum">     133 </span><span class="lineCov">     119514 : static TString *internshrstr (lua_State *L, const char *str, size_t l) {</span>
<span class="lineNum">     134 </span>            :   GCObject *o;
<span class="lineNum">     135 </span><span class="lineCov">     119514 :   global_State *g = G(L);</span>
<span class="lineNum">     136 </span><span class="lineCov">     119514 :   unsigned int h = luaS_hash(str, l, g-&gt;seed);</span>
<span class="lineNum">     137 </span><span class="lineCov">     169047 :   for (o = g-&gt;strt.hash[lmod(h, g-&gt;strt.size)];</span>
<span class="lineNum">     138 </span>            :        o != NULL;
<span class="lineNum">     139 </span><span class="lineCov">      49533 :        o = gch(o)-&gt;next) {</span>
<span class="lineNum">     140 </span><span class="lineCov">     127896 :     TString *ts = rawgco2ts(o);</span>
<span class="lineNum">     141 </span><span class="lineCov">     127896 :     if (h == ts-&gt;tsv.hash &amp;&amp;</span>
<span class="lineNum">     142 </span><span class="lineCov">      78414 :         l == ts-&gt;tsv.len &amp;&amp;</span>
<span class="lineNum">     143 </span><span class="lineCov">      78414 :         (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {</span>
<span class="lineNum">     144 </span><span class="lineCov">      78363 :       if (isdead(G(L), o))  /* string is dead (but was not collected yet)? */</span>
<span class="lineNum">     145 </span><span class="lineCov">        111 :         changewhite(o);  /* resurrect it */</span>
<span class="lineNum">     146 </span><span class="lineCov">      78363 :       return ts;</span>
<span class="lineNum">     147 </span>            :     }
<span class="lineNum">     148 </span>            :   }
<span class="lineNum">     149 </span><span class="lineCov">      41151 :   return newshrstr(L, str, l, h);  /* not found; create a new string */</span>
<span class="lineNum">     150 </span>            : }
<span class="lineNum">     151 </span>            : 
<span class="lineNum">     152 </span>            : 
<span class="lineNum">     153 </span>            : /*
<span class="lineNum">     154 </span>            : ** new string (with explicit length)
<span class="lineNum">     155 </span>            : */
<span class="lineNum">     156 </span><span class="lineCov">     123360 : TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {</span>
<span class="lineNum">     157 </span><span class="lineCov">     123360 :   if (l &lt;= LUAI_MAXSHORTLEN)  /* short string? */</span>
<span class="lineNum">     158 </span><span class="lineCov">     119514 :     return internshrstr(L, str, l);</span>
<span class="lineNum">     159 </span>            :   else {
<span class="lineNum">     160 </span><span class="lineCov">       3846 :     if (l + 1 &gt; (MAX_SIZET - sizeof(TString))/sizeof(char))</span>
<span class="lineNum">     161 </span><span class="lineNoCov">          0 :       luaM_toobig(L);</span>
<span class="lineNum">     162 </span><span class="lineCov">       3846 :     return createstrobj(L, str, l, LUA_TLNGSTR, G(L)-&gt;seed, NULL);</span>
<span class="lineNum">     163 </span>            :   }
<span class="lineNum">     164 </span>            : }
<span class="lineNum">     165 </span>            : 
<span class="lineNum">     166 </span>            : 
<span class="lineNum">     167 </span>            : /*
<span class="lineNum">     168 </span>            : ** new zero-terminated string
<span class="lineNum">     169 </span>            : */
<span class="lineNum">     170 </span><span class="lineCov">      33138 : TString *luaS_new (lua_State *L, const char *str) {</span>
<span class="lineNum">     171 </span><span class="lineCov">      33138 :   return luaS_newlstr(L, str, strlen(str));</span>
<span class="lineNum">     172 </span>            : }
<span class="lineNum">     173 </span>            : 
<span class="lineNum">     174 </span>            : 
<span class="lineNum">     175 </span><span class="lineCov">        386 : Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {</span>
<span class="lineNum">     176 </span>            :   Udata *u;
<span class="lineNum">     177 </span><span class="lineCov">        386 :   if (s &gt; MAX_SIZET - sizeof(Udata))</span>
<span class="lineNum">     178 </span><span class="lineNoCov">          0 :     luaM_toobig(L);</span>
<span class="lineNum">     179 </span><span class="lineCov">        386 :   u = &amp;luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)-&gt;u;</span>
<span class="lineNum">     180 </span><span class="lineCov">        385 :   u-&gt;uv.len = s;</span>
<span class="lineNum">     181 </span><span class="lineCov">        385 :   u-&gt;uv.metatable = NULL;</span>
<span class="lineNum">     182 </span><span class="lineCov">        385 :   u-&gt;uv.env = e;</span>
<span class="lineNum">     183 </span><span class="lineCov">        385 :   return u;</span>
<span class="lineNum">     184 </span>            : }
<span class="lineNum">     185 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
